package com.eqdd.yiqidian.ui.tongxunlu;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.TeamRemoveActivityCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.bean.rebuild.GroupMemberListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.List;

import io.reactivex.Flowable;

public class TeamRemoveActivity extends BaseActivity {
    private TeamRemoveActivityCustom dataBinding;
    private String groupid;
    private SlimAdapterEx<GroupMemberListResultBean> slimAdapterEx;
    private String groupname;
    private StringBuilder stringBuilder;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_team_remove);
        initTopTitleBar(View.VISIBLE, "踢人");
        initTopRightText("确定", v -> {
            String guids = getGuids();
            if (!TextUtils.isEmpty(guids)) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.QUIT_GROUP)
                        .params("userGuid",guids)
                        .params("Groupid", groupid)
                        .params("GroupName", groupname)
                        .execute(new DialogCallBack<HttpResult>(this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    setView();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort("请先选定人");
            }
        });
    }

    private String getGuids() {
        stringBuilder = new StringBuilder();
        Flowable.fromIterable(slimAdapterEx.getData())
                .filter(bean -> bean.isChecked())
                .subscribe(bean -> stringBuilder.append(";" + bean.getGuid()));
        return stringBuilder.toString();
    }


    @Override
    public void initData() {
        groupid = getIntent().getStringExtra("groupid");
        groupname = getIntent().getStringExtra("groupname");
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_04, new SlimInjector<GroupMemberListResultBean>() {
            @Override
            public void onInject(GroupMemberListResultBean data, IViewInjector injector) {
                injector.text(R.id.tv_content, data.getUpname())
                        .with(R.id.iv_head, v ->
                                ImageUtil.setCircleImage(data.getIphoto(), (ImageView) v))
                        .checked(R.id.checkbox, data.isChecked());

            }
        })
                .attachTo(dataBinding.recyclerView);
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
                    @Override
                    public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                        slimAdapterEx.getData().get(position).setChecked(!slimAdapterEx.getData().get(position).isChecked());
                        slimAdapterEx.notifyItemChanged(position);
                    }
                });
    }

    @Override
    public void setView() {
        OkGo.<HttpResult<List<GroupMemberListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.GROUP_USER_LIST)
                .params("groupid", groupid)
                .execute(new DialogCallBack<HttpResult<List<GroupMemberListResultBean>>>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<GroupMemberListResultBean>>> response) {
                        HttpResult<List<GroupMemberListResultBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            Flowable.fromIterable(httpResult.getItems())
                                    .filter(bean->bean.getGuid().equals(user.getGuid()))
                                    .subscribe(bean->httpResult.getItems().remove(bean),e-> System.out.println(e.toString()));
                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
